//
//  LXReturnCarDetailViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/24.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXReturnCarDetailViewController.h"
#import "LXTableViewCell.h"
#import "LXAnnexShowTableViewCell.h"
#import "LXRemarkTabelViewCell.h"
@interface LXReturnCarDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSArray *data;
@property (nonatomic) NSMutableDictionary *loanDic;
@property (nonatomic) NSDictionary *selDic;

@end

@implementation LXReturnCarDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = White_Color;

    [self layoutCustomNav:self.view title:@"退车详情"];
    

    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerClass:[LXAnnexShowTableViewCell class]
         forCellReuseIdentifier:NSStringFromClass([LXAnnexShowTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
    
    
    [self.view addSubview:self.tableView];
    
    [self loadData];
    

    
}
- (void)loadData{
    
    self.loanDic  = [NSMutableDictionary dictionary];
    
    
    [self.loanDic setValue:self.carModel.carNumber forKey:@"carNumber"];
    [self.loanDic setValue:[NSString stringWithFormat:@"%@ %@",self.carModel.carBrand,self.carModel.carModel] forKey:@"carBrand"];
    
    if (self.carModel.returnType == 0) {
        [self.loanDic setValue:@"正常退车" forKey:@"returnType"];
    }
    else
    {
        [self.loanDic setValue:@"违约退车" forKey:@"returnType"];
    }
    [self.loanDic setValue:self.carModel.remark forKey:@"remark"];

    [self.loanDic setValue:self.carModel.customerName forKey:@"customerName"];
    [self.loanDic setValue:self.carModel.customerPhone forKey:@"customerPhone"];
    [self.loanDic setValue:self.carModel.carReturnTime forKey:@"carReturnTime"];
    [self.loanDic setValue:self.carModel.carManagerName forKey:@"carManagerName"];
    [self.loanDic setValue:@"已退车" forKey:@"carReturnState"];
    [self.loanDic setValue:self.carModel.refundDeposit forKey:@"refundDeposit"];
    [self.loanDic setValue:StringIsEmpty(self.carModel.deductBreach)?@"0":self.carModel.deductBreach forKey:@"deductBreach"];
    [self.loanDic setValue:StringIsEmpty(self.carModel.deductDamage)?@"0":self.carModel.deductDamage forKey:@"deductDamage"];
    [self.loanDic setValue:StringIsEmpty(self.carModel.deductIllegal)?@"0":self.carModel.deductIllegal forKey:@"deductIllegal"];
    [self.loanDic setValue:StringIsEmpty(self.carModel.deductOther)?@"0":self.carModel.deductOther forKey:@"deductOther"];
    [self.loanDic setValue:self.carModel.remark forKey:@"remark"];

    

    self.data = @[
        @{
            @"title":@"车牌号",
            @"key":@"carNumber",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"品牌型号",
            @"key":@"carBrand",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"客户姓名",
            @"key":@"customerName",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"手机号",
            @"key":@"customerPhone",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"退车日期",
            @"key":@"carReturnTime",
            @"height":@(kComHeight),
        },
        @{
            @"title":@"处理人",
            @"key":@"carManagerName",
            @"height":@(kComHeight),
        },
        @{
            @"title":@"解约状态",
            @"key":@"returnType",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"退车状态",
            @"key":@"carReturnState",
            @"height":@(kComHeight),
        },
        @{
            @"title":@"退押金(元)",
            @"key":@"refundDeposit",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"违约金(元)",
            @"key":@"deductBreach",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"车损金额(元)",
            @"key":@"deductDamage",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"待处理违章金额(元)",
            @"key":@"deductIllegal",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"其他金额(元)",
            @"key":@"deductOther",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"备注",
            @"key":@"remark",
            @"height":@([NSString remarksHeight:self.carModel.remark])
        },
        @{
            @"title":@"退车附加图片",
            @"key":@"annex",
            @"value":self.carModel.pics,
            @"height":@([LXAnnexShowTableViewCell getCellHeightWithDataSource:self.carModel.pics])
        },
        
        
    ];

    
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.row];
   
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.row];

    // 附件
    if ([dic[@"key"] isEqualToString:@"annex"]) {
        LXAnnexShowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXAnnexShowTableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        cell.titleLab.text = dic[@"title"];
        
        NSArray *ary = dic[@"value"];
        [cell reloadData:ary];
        
        // 放大图片
        [cell setCallBackExtendAnnex:^(NSInteger num,id obj) {
            LXBigImageView *view = [[LXBigImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) WithShowImageAry:ary];
            [view.scrollView setContentOffset:CGPointMake(ScreenWidth*num, 0) animated:NO];
            [[UIApplication sharedApplication].delegate.window addSubview:view];
            
        }];
       
        
        return cell;
    }
    // 备注
   else if([dic[@"key"]  isEqualToString:@"remark"]){
       LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
       cell.textView.wordNumLabel.hidden = YES;
       cell.remarkTitle.text = dic[@"title"];
       cell.textView.text = !StringIsEmpty(self.loanDic[dic[@"key"]])?self.loanDic[dic[@"key"]]:@"无";
       cell.textView.userInteractionEnabled = NO;

       return cell;
   }
    
       
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    

    cell.cellDic = dic;
    cell.textFiled.hidden = YES;
    cell.subTitleLab.hidden = NO;
    cell.subTitleLab.text = self.loanDic[dic[@"key"]];
    

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = self.data[indexPath.section][indexPath.row];
    self.selDic = dic;
    NSString * selName = dic[@"sel"];
    SEL action = NSSelectorFromString(selName);
    if ([self respondsToSelector:action]) {
        [self performSelector:action withObject:nil afterDelay:0];
    }

}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, kScreenHeight - kViewTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FA"];
        _tableView.sectionHeaderHeight = 10;
        _tableView.sectionFooterHeight = 0.00001;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


@end
